package com.yanqu.road.dao.impl.activity.ruishou;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.ruishou.RuiShouAreaData;
import com.yanqu.road.entity.activity.ruishou.RuiShouFightMonsterData;
import com.yanqu.road.entity.activity.ruishou.RuiShouMonsterData;
import com.yanqu.road.entity.activity.ruishou.RuiShouUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

public class RuiShouAreaDataDaoImpl extends TempDataObjectDao<RuiShouAreaData> {

    public static String getRuiShouAreaDataMapSql = "select * from `t_u_activity_rui_shou_area_data` where `activityId`=? and `userId`=?";

    public Map<Integer, RuiShouAreaData> getRuiShouAreaDataMap(int activityId, long userId) {
        Map<Integer, RuiShouAreaData> map = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getRuiShouAreaDataMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                RuiShouAreaData areaData = parse(rs);
                map.put(areaData.getAreaId(), areaData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertRuiShouAreaDataSql = "insert into `t_u_activity_rui_shou_area_data` (activityId,userId,areaId,fightMonsterDataMap,reset,lockReward,monsterDataMap,actionIdPool) values (?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(RuiShouAreaData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAreaId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getFightMonsterDataMap())));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isReset()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isLockReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getMonsterDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getActionIdPool())));
        if (getDb().executeNoneQuery(insertRuiShouAreaDataSql, para) > 0) {
            return true;
        }
        return false;
    }


    public static String updateRuiShouAreaDataSql = "update `t_u_activity_rui_shou_area_data` set `fightMonsterDataMap`=?,`reset`=?,`lockReward`=?,`monsterDataMap`=?,`actionIdPool`=? where `activityId`=? and `userId`=? and `areaId`=? ";

    @Override
    protected boolean updateData(RuiShouAreaData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getFightMonsterDataMap())));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isReset()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isLockReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getMonsterDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getActionIdPool())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAreaId()));
        if (getDb().executeNoneQuery(updateRuiShouAreaDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected RuiShouAreaData parseData(ResultSet rs) throws SQLException {
        RuiShouAreaData data = new RuiShouAreaData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setAreaId(rs.getInt("areaId"));
        String fightMonsterDataMap = rs.getString("fightMonsterDataMap");
        if (!StringUtils.isNullOrEmpty(fightMonsterDataMap)) {
            data.setFightMonsterDataMap(JSON.parseObject(fightMonsterDataMap, new TypeReference<Map<Integer, RuiShouFightMonsterData>>() {
            }));
        }
        data.setReset(rs.getBoolean("reset"));
        data.setLockReward(rs.getBoolean("lockReward"));
        String monsterDataMap = rs.getString("monsterDataMap");
        if (!StringUtils.isNullOrEmpty(monsterDataMap)) {
            data.setMonsterDataMap(JSON.parseObject(monsterDataMap, new TypeReference<Map<Integer, RuiShouMonsterData>>() {
            }));
        }
        String actionIdPool = rs.getString("actionIdPool");
        if (!StringUtils.isNullOrEmpty(actionIdPool)) {
            data.setActionIdPool(JSON.parseObject(actionIdPool, new TypeReference<LinkedList<Integer>>() {
            }));
        }
        return data;
    }
}
